package com.groot.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.groot.common.util.MedicalDBUtil;
import com.groot.medical.entity.AdministrativeArea;
import com.groot.medical.entity.RuralInstitution;

public class InstitutionDaoImpl implements InstitutionDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	@Override
	public int getAllInstitutionCount() {
		int i =0;
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
		conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
		String sql="SELECT count(1) AS id FROM t_institution";
		ps=conn.prepareStatement(sql);
		rs=ps.executeQuery();
		if(rs.next()){
			i=rs.getInt("id");
		}
		} catch (SQLException e) {

		e.printStackTrace();
		}
		finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
		return i;
	}

	@Override
	public List<RuralInstitution> findInstitutionCByPage(int startIndex, int pageSize) {
	
		String sql = "select * from t_institution limit ?,?";
		
		try {
			conn = MedicalDBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, startIndex);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			List<RuralInstitution> institutionList = new ArrayList<>();
			
			while(rs.next()){
				RuralInstitution institution = new RuralInstitution();
				institution.setId(rs.getInt(1));
				institution.setInstitutionId(rs.getString(2));
				institution.setInstitutionName(rs.getString(3));
				institution.setInstitutionLevel(rs.getString(4));
				institution.setInstitutionAddress(rs.getString(5));
				institution.setAreacode(rs.getString(6));
				institutionList.add(institution);
				
			}
			
			return institutionList;
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new  RuntimeException("出现错误！");
		}finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
	}

	@Override
	public int addInstitutionInfo(RuralInstitution institution) {
		int i =0;
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			String sql="insert into t_institution(institution_id,institution_name,institutioin_level,institution_address,areacode)values(?,?,?,?,?)";
			
			ps=conn.prepareStatement(sql);
			ps.setString(1,institution.getInstitutionId() );
			ps.setString(2,institution.getInstitutionName());
			ps.setString(3,institution.getInstitutionLevel());
			ps.setString(4,institution.getInstitutionAddress());
			ps.setString(5,institution.getAreacode());
			
			i= ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
			return i;
	}

	@Override
	public RuralInstitution findInstitutionInfoById(String id) {
		String sql = "select * from t_institution where id = ?";
		RuralInstitution institution = new RuralInstitution();
		try {
			conn = MedicalDBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			rs = ps.executeQuery();
			
			
			while(rs.next()){
				
				institution.setId(rs.getInt(1));
				institution.setInstitutionId(rs.getString(2));
				institution.setInstitutionName(rs.getString(3));
				institution.setInstitutionLevel(rs.getString(4));
				institution.setInstitutionAddress(rs.getString(5));
				institution.setAreacode(rs.getString(6));
			
			}
			
			return institution;
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new  RuntimeException("出现错误！");
		}finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
	}

	@Override
	public void editInstitutionInfo(RuralInstitution institution) {
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			//UPDATE student SET name='"+name+"', age=" + age+" where name='大红'";
			String sql="update t_institution set institution_id=?,institution_name=?,institution_address=? where id=?";
			ps=conn.prepareStatement(sql);
			
			ps.setString(1, institution.getInstitutionId());
			ps.setString(2, institution.getInstitutionName());
			ps.setString(3,institution.getInstitutionAddress());
			ps.setInt(4,institution.getId());
			ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
		
		
	}

}
